<template>
  <div class="login-container">
    <div class="login-form">
      <h2 class="form-title">登录<span class="form-subtitle">Welcome back!</span></h2>
      <div class="form-group">
        <label for="username">手机号</label>
        <input type="text" id="username" v-model="username" placeholder="请输入手机号">
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input type="password" id="password" v-model="password" placeholder="请输入密码">
      </div>
      <div class="form-button">
        <button class="btn-login" @click="login">登录</button>
      </div>
      <div class="form-footer">
  <router-link to="/register" class="link-create">注册账号</router-link>
  <!-- <router-link to="/infoCus" class="link-create">测试资料编辑</router-link> -->
</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      remember: false
    }
  },
  methods: {
    login() {
      
      //  this.$router.push('/DataSets')
      // 处理登录逻辑
      console.log('login', this.username, this.password)
      this.$axios.get('http://localhost/api/user/login',
      {
        params: {
          phone: this.username,
          password: this.password
        }
      }
      ).then(res => {
        console.log(res.data)
        if (res.data.code === '200' || res.data.data !== null) {
          //在sessionstorage中存储后端返回的令牌
          sessionStorage.setItem('token', res.data.data)
          console.log('token保存成功' + sessionStorage.getItem('token'))
          console.log(res)
          alert('登录成功')
          this.$router.push('/DataSets')
        } else {
          alert('密码错误，请重新输入密码！')
        }
      }).catch(err => {
        alert('连接失败,请检查网络')
        console.log('出错了' + err)
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background-color: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-form {
  width: 400px;
  background-color: rgba(255, 255, 255, 0.6);
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(0,0,0,.1);
  padding: 40px;
}

.form-title {
  font-size: 24px;
  line-height: 1.2;
  margin-bottom: 16px;
}

.form-subtitle {
  font-size: 14px;
  color: #ffffff;
  margin-left: 8px;
}

.form-group {
  margin-bottom: 24px;
}

.form-group label {
  display: block;
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
}

.form-group input[type="text"],
.form-group input[type="password"] {
  display: block;
  width: 100%;
  height: 36px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 8px;
  font-size: 14px;
  color: #333;
}

.btn-login {
  background-color: #3a5344;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 12px 24px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color .3s ease-in-out;
}

.btn-login:hover {
  background-color: #83c19d;
}

.form-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 16px;
  font-size: 14px;
  color: #999999;
}

.form-footer label {
  margin-right: 16px;
}

.link-create {
  color: #1c442c;
  text-decoration: none;
  transition: color .3s ease-in-out;
}

.link-create:hover {
  color: #7a9b87;
}
.form-group {
  display: flex;
  align-items: center;
}

.form-group label {
  width: 80px;
  margin-right: 16px;
}

.form-group input[type="text"],
.form-group input[type="password"] {
  flex: 1;
}
</style>
